package com.example.gamecenter.adapter

import ListRecommendAdapter
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.gamecenter.R
import com.example.gamecenter.activitys.DetailActivity
import com.example.gamecenter.utils.Utility
import com.example.nanke.adapter.ListRecommendAdapter2

class ListHomeRecommendCardAdapter(private val context: Context, bgTransprent: Boolean? = false) : BaseAdapter() {
    val bgTransprent: Boolean? = bgTransprent
    private var datas: ArrayList<HomeRecommendCardItem> = arrayListOf();
    // 将点击事件都回调到上层
    // 点击回调接口
    interface OnRecommendItemClickListener {
        /** 更多点击事件 */
        fun onRecommendItemMoreClick(v: View?, parmas: HomeRecommendCardItem?=null)
    }
    private var onRecommendItemClickListener: OnRecommendItemClickListener? = null;

    fun setOnRecommendItemClickListener(listener: OnRecommendItemClickListener) {
        onRecommendItemClickListener = listener
    }
    fun setDatas(d: List<HomeRecommendCardItem>) {
        datas.clear()
        datas.addAll(d)
        this.notifyDataSetChanged()
    }
    override fun getCount(): Int {
        return datas.size
    }

    override fun getItem(position: Int): Any {
        return datas[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view: View= convertView ?: LayoutInflater.from(context).inflate(R.layout.home_recommend_card_item, parent, false);
        val textView: TextView = view.findViewById(R.id.home_card_title);
        val viewMore: ImageView = view.findViewById(R.id.view_more);
        val item: HomeRecommendCardItem = datas[position];
        textView.text = item.typeName;
        println("sout: " + item.vertical)
        viewMore.setOnClickListener(object :View.OnClickListener{
            override fun onClick(p0: View?) {
                // Log.e("nanke", "12344")
                // 调用回调接口的方法
                onRecommendItemClickListener?.onRecommendItemMoreClick(p0, item)
            }
        })
        if (item.vertical){// position % 2 == 0 是偶数 || 垂直布局
            // 今日推荐list 垂直排列
            val recommendListView = view.findViewById<ListView>(R.id.recommend_list_view)
            val recommendAdapter = ListRecommendAdapter(context, item.recommendList)
            recommendListView.visibility = View.VISIBLE
            recommendListView.adapter = recommendAdapter

            recommendAdapter.setOnRecommendItemClickListener(object : ListRecommendAdapter.OnRecommendItemClickListener{
                override fun toDetail(v: View?, parmas: RecommendItem?) {
                    // 处理点击事件
                    if (v?.id == R.id.recommend_item_view) { // 查看更多
                        // Log.e("nanke", v?.id.toString())
                        val intent = Intent(context, DetailActivity().javaClass)
//                        intent.putExtra("typeName", "${parmas?.id}")
                        intent.putExtra("id", parmas?.id)
                        v?.context.startActivity(intent)
                    }
                }
            })
            // 计算listview的高度并且撑开，不然会收缩成一个，需要先建立MyListView
            Utility.setListViewHeightBasedOnChildren(recommendListView)
        } else { // 是奇数
            // 水平排列
            val recyclerView: RecyclerView = view.findViewById(R.id.recycler_view)
            val linearLayoutManager =
                LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
            val adapter = ListRecommendAdapter2(item.recommendList)
            val transparent = this.bgTransprent ?: false;
            if (transparent) {
                val cardView: CardView = view.findViewById(R.id.home_card_view)
                cardView.setCardBackgroundColor(Color.TRANSPARENT)
            }

            recyclerView.visibility = View.VISIBLE
            recyclerView.layoutManager = linearLayoutManager
            recyclerView.adapter = adapter
        }
        return view
    }
}
data class RecommendItem (
    val title :String,
    val imageUrl :String,
    val id :Int,
    val desc :String
)
data class HomeRecommendCardItem(
    val typeName: String,
    val typeId: Int,
    val vertical: Boolean,
    val recommendList: List<RecommendItem>
)